package com.glinsoft.cg.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 字段类型转换
 */
public class SQLTypes {

    private static Map<String, String> javaTypeMap = new HashMap<String, String>();
    private static Map<String, String> javaBoxTypeMap = new HashMap<String, String>();
    private static Map<String, String> mybatisTypeMap = new HashMap<String, String>();

    static {
        javaTypeMap.put("bigint", "long");
        javaTypeMap.put("binary", "byte[]");
        javaTypeMap.put("bit", "boolean");
        javaTypeMap.put("blob", "byte[]");
        javaTypeMap.put("char", "String");
        javaTypeMap.put("date", "LocalDate");
        javaTypeMap.put("datetime", "LocalDateTime");
        javaTypeMap.put("decimal", "BigDecimal");
        javaTypeMap.put("double", "double");
        javaTypeMap.put("float", "float");
        javaTypeMap.put("int", "int");
        javaTypeMap.put("image", "byte[]");
        javaTypeMap.put("money", "BigDecimal");
        javaTypeMap.put("nchar", "String");
        javaTypeMap.put("ntext", "byte");
        javaTypeMap.put("numeric", "BigDecimal");
        javaTypeMap.put("nvarchar", "String");
        javaTypeMap.put("real", "float");
        javaTypeMap.put("smalldatetime", "LocalDateTime");
        javaTypeMap.put("smallint", "int");
        javaTypeMap.put("smallmoney", "BigDecimal");
        javaTypeMap.put("sql_variant", "String");
        javaTypeMap.put("text", "String");
        javaTypeMap.put("tinyint", "int");
        javaTypeMap.put("timestamp", "LocalDateTime");
        javaTypeMap.put("time", "String");
        javaTypeMap.put("year", "String");
        javaTypeMap.put("uniqueidentifier", "String");
        javaTypeMap.put("varbinary", "byte[]");
        javaTypeMap.put("varchar", "String");

        javaBoxTypeMap.put("bigint", "Long");
        javaBoxTypeMap.put("binary", "Byte[]");
        javaBoxTypeMap.put("bit", "Boolean");
        javaBoxTypeMap.put("blob", "Byte[]");
        javaBoxTypeMap.put("char", "String");
        javaBoxTypeMap.put("date", "LocalDate");
        javaBoxTypeMap.put("datetime", "LocalDateTime");
        javaBoxTypeMap.put("time", "String");
        javaBoxTypeMap.put("decimal", "BigDecimal");
        javaBoxTypeMap.put("double", "Double");
        javaBoxTypeMap.put("float", "Float");
        javaBoxTypeMap.put("int", "Integer");
        javaBoxTypeMap.put("image", "Byte[]");
        javaBoxTypeMap.put("money", "BigDecimal");
        javaBoxTypeMap.put("nchar", "String");
        javaBoxTypeMap.put("ntext", "String");
        javaBoxTypeMap.put("numeric", "BigDecimal");
        javaBoxTypeMap.put("nvarchar", "String");
        javaBoxTypeMap.put("real", "Float");
        javaBoxTypeMap.put("smalldatetime", "LocalDateTime");
        javaBoxTypeMap.put("smallint", "Integer");
        javaBoxTypeMap.put("smallmoney", "BigDecimal");
        javaBoxTypeMap.put("sql_variant", "String");
        javaBoxTypeMap.put("text", "String");
        javaBoxTypeMap.put("tinyint", "int");
        javaBoxTypeMap.put("timestamp", "LocalDateTime");
        javaBoxTypeMap.put("uniqueidentifier", "String");
        javaBoxTypeMap.put("varbinary", "Byte[]");
        javaBoxTypeMap.put("varchar", "String");
        //oracle
        javaBoxTypeMap.put("varchar2", "String");

        mybatisTypeMap.put("bigint", "BIGINT");
        mybatisTypeMap.put("binary", "BLOB");
        mybatisTypeMap.put("bit", "BOOLEAN");
        mybatisTypeMap.put("blob", "BLOB");
        mybatisTypeMap.put("char", "CHAR");
        mybatisTypeMap.put("date", "TIMESTAMP");
        mybatisTypeMap.put("datetime", "TIMESTAMP");
        mybatisTypeMap.put("time", "VARCHAR");
        mybatisTypeMap.put("decimal", "DECIMAL");
        mybatisTypeMap.put("double", "DOUBLE");
        mybatisTypeMap.put("float", "FLOAT");
        mybatisTypeMap.put("int", "INTEGER");
        mybatisTypeMap.put("image", "BLOB");
        mybatisTypeMap.put("money", "DECIMAL");
        mybatisTypeMap.put("nchar", "NCHAR");
        mybatisTypeMap.put("ntext", "VARCHAR");
        mybatisTypeMap.put("numeric", "DECIMAL");
        mybatisTypeMap.put("nvarchar", "NVARCHAR");
        mybatisTypeMap.put("real", "FLOAT");
        mybatisTypeMap.put("smalldatetime", "TIMESTAMP");
        mybatisTypeMap.put("smallint", "INTEGER");
        mybatisTypeMap.put("smallmoney", "DECIMAL");
        mybatisTypeMap.put("sql_variant", "VARCHAR");
        mybatisTypeMap.put("text", "VARCHAR");
        mybatisTypeMap.put("tinyint", "TINYINT");
        mybatisTypeMap.put("timestamp", "TIMESTAMP");
        mybatisTypeMap.put("uniqueidentifier", "VARCHAR");
        mybatisTypeMap.put("varbinary", "BLOB");
        mybatisTypeMap.put("varchar", "VARCHAR");
        mybatisTypeMap.put("varchar2", "VARCHAR");
    }

    /**
     * 将SQL字段类型转换为JAVA字段类型
     *
     * @param sqlType
     * @return 找不到类型默认返回String
     */
    public static String convertToJavaType(String sqlType) {
        String javaType = javaTypeMap.get(sqlType);
        return javaType == null ? "String" : javaType;
    }

    /**
     * 将SQL字段类型转换为JAVA装箱字段类型
     *
     * @param sqlType
     * @return 找不到类型默认返回String
     */
    public static String convertToJavaBoxType(String sqlType) {
        String javaType = javaBoxTypeMap.get(sqlType);
        return javaType == null ? "String" : javaType;
    }

    /**
     * 将SQL字段类型转换为mybatis的jdbcType
     *
     * @param sqlType
     * @return 找不到类型默认返回VARCHAR
     */
    public static String convertToMyBatisJdbcType(String sqlType) {
        String javaType = mybatisTypeMap.get(sqlType);
        return javaType == null ? "VARCHAR" : javaType;
    }

}
